# clear working environment
rm(list = ls())

#### load the required packages ####
library(tidyverse)
library(PanelMatch)
library(countrycode)

#### load the clean data ####
df <- read_rds("data/dem-transitions-replication-data.rds") %>%
  glimpse()

#### make df into a properly formatted dataframe for PanelMatch ####

# begin coercing df into m
# df needs to be a dataframe class
m <- as.data.frame(df)
# check to see if m is a dataframe
class(m)

# country needs to be an integer to use PanelMatch
m$country <- as.integer(m$country)

# year needs to be an integer to use PanelMatch
m$year <- as.integer(m$year)

# create placebo treatment variables
m <- m %>%
  group_by(country) %>%
  mutate(regime_lead_4 = dplyr::lead(v2x_regime, 4)) %>%
  mutate(regime_lead_6 = dplyr::lead(v2x_regime, 6)) %>%
  mutate(year_lead_4 = dplyr::lead(year, 4)) %>%
  mutate(year_lead_6 = dplyr::lead(year, 6)) %>%
  ungroup() %>%
  glimpse()

m$pl_1 <- as.numeric(ifelse(m$regime_lead_4 >= 2, 1, 0))
m$pl_2 <- as.numeric(ifelse(m$regime_lead_6 >= 2, 1, 0))

# all other variables, except categorical variables, need to be numeric
m$mid <- as.numeric(m$mid)
m$fatal <- as.numeric(m$fatal)
m$ln_cinc <- as.numeric(m$ln_cinc)
m$ln_pecpp <- as.numeric(m$ln_pecpp)
m$terr_dispute <- as.numeric(m$terr_dispute)


# make sure all of the rows are unique
m <- distinct(m, country, year, .keep_all = TRUE)

# df needs to be a dataframe class
m <- as.data.frame(m)
# check to see if m is a dataframe
class(m)

# set seed to reproduce bootstrapped estimates
set.seed(1)

#### effect of placebo 1 democratic transition on new MID participation (pooled three years) ####
# create panel data
panel <- PanelData(panel.data = m,
                   unit.id = "country",
                   time.id = "year",
                   treatment = "pl_1",
                   outcome = "mid")

# create matched sets
PMresults.mid.att <- PanelMatch(panel.data = panel, lag = 5, refinement.method = "CBPS.weight",
                                match.missing = TRUE, exact.match.variables = c("un_region"),
                                covs.formula = ~ I(lag(mid, 1:5)) + I(lag(ln_cinc, 1:5)) + I(lag(ln_pecpp, 1:5)) + I(lag(terr_dispute, 1:5)),
                                qoi = "att", lead = 1:3, forbid.treatment.reversal = FALSE,
                                use.diagonal.variance.matrix = TRUE)

# estimate the ATT
PEresults.mid.3.att <- PanelEstimate(panel.data = panel,
                                     sets = PMresults.mid.att,
                                     se.method = "bootstrap", number.iterations = 10000,
                                     confidence.level = .90,
                                     pooled = TRUE)

#### effect of placebo 2 democratic transition on new MID participation (pooled three years) ####
# create panel data
panel <- PanelData(panel.data = m,
                   unit.id = "country",
                   time.id = "year",
                   treatment = "pl_2",
                   outcome = "mid")

# create matched sets
PMresults.mid.att <- PanelMatch(panel.data = panel, lag = 5, refinement.method = "CBPS.weight",
                                match.missing = TRUE, exact.match.variables = c("un_region"),
                                covs.formula = ~ I(lag(mid, 1:5)) + I(lag(ln_cinc, 1:5)) + I(lag(ln_pecpp, 1:5)) + I(lag(terr_dispute, 1:5)),
                                qoi = "att", lead = 1:3, forbid.treatment.reversal = FALSE,
                                use.diagonal.variance.matrix = TRUE)

# estimate the ATT
PEresults.mid.5.att <- PanelEstimate(panel.data = panel, 
                                     sets = PMresults.mid.att, 
                                     se.method = "bootstrap", number.iterations = 10000,
                                     confidence.level = .90,
                                     pooled = TRUE)

#### effect of placebo 1 democratic transition on new fatal MID participation (pooled three years) ####
# create panel data
panel <- PanelData(panel.data = m,
                   unit.id = "country",
                   time.id = "year",
                   treatment = "pl_1",
                   outcome = "fatal")

# create matched sets
PMresults.fatal.att <- PanelMatch(panel.data = panel, lag = 5, refinement.method = "CBPS.weight",
                                  match.missing = TRUE, exact.match.variables = c("un_region"),
                                  covs.formula = ~ I(lag(fatal, 1:5)) + I(lag(ln_cinc, 1:5)) + I(lag(ln_pecpp, 1:5)) + I(lag(terr_dispute, 1:5)),
                                  qoi = "att", lead = 1:3, forbid.treatment.reversal = FALSE,
                                  use.diagonal.variance.matrix = TRUE)

# estimate the ATT
PEresults.fatal.3.att <- PanelEstimate(panel.data = panel, 
                                       sets = PMresults.fatal.att, 
                                       se.method = "bootstrap", number.iterations = 10000,
                                       confidence.level = .90,
                                       pooled = TRUE)

#### effect of placebo 2 democratic transition on new fatal MID participation (pooled five years) ####
# create panel data
panel <- PanelData(panel.data = m,
                   unit.id = "country",
                   time.id = "year",
                   treatment = "pl_2",
                   outcome = "fatal")

# create matched sets
PMresults.fatal.att <- PanelMatch(panel.data = panel, lag = 5, refinement.method = "CBPS.weight",
                                  match.missing = TRUE, exact.match.variables = c("un_region"),
                                  covs.formula = ~ I(lag(fatal, 1:5)) + I(lag(ln_cinc, 1:5)) + I(lag(ln_pecpp, 1:5)) + I(lag(terr_dispute, 1:5)),
                                  qoi = "att", lead = 1:3, forbid.treatment.reversal = FALSE,
                                  use.diagonal.variance.matrix = TRUE)

# estimate the ATT
PEresults.fatal.5.att <- PanelEstimate(panel.data = panel, 
                                       sets = PMresults.fatal.att,
                                       se.method = "bootstrap", number.iterations = 10000,
                                       confidence.level = .90,
                                       pooled = TRUE)

#### plot placebo tests estimates ####
# create data frame to plot 
gg <- data.frame(outcome = c("All MIDs", "All MIDs", "Fatal MIDs", "Fatal MIDs"),
                 number_periods = c("3", "5", "3", "5"),
                 estimates = c(PEresults.mid.3.att[["estimate"]], PEresults.mid.5.att[["estimate"]], PEresults.fatal.3.att[["estimate"]], PEresults.fatal.5.att[["estimate"]]),
                 boot_lo = c(quantile(PEresults.mid.3.att[["bootstrapped.estimates"]], probs = 0.05), quantile(PEresults.mid.5.att[["bootstrapped.estimates"]], probs = 0.05), quantile(PEresults.fatal.3.att[["bootstrapped.estimates"]], probs = 0.05), quantile(PEresults.fatal.5.att[["bootstrapped.estimates"]], probs = 0.05)),
                 boot_hi = c(quantile(PEresults.mid.3.att[["bootstrapped.estimates"]], probs = 0.95), quantile(PEresults.mid.5.att[["bootstrapped.estimates"]], probs = 0.95), quantile(PEresults.fatal.3.att[["bootstrapped.estimates"]], probs = 0.95), quantile(PEresults.fatal.5.att[["bootstrapped.estimates"]], probs = 0.95)))

# create plot for Figure F.1 Panel B
id_panel_b <- ggplot(data = gg, aes(x = outcome)) +
  geom_hline(aes(yintercept = 0), linetype = "dashed", linewidth = .25) +
  geom_pointrange(aes(y = estimates, ymin = boot_lo, ymax = boot_hi, shape = number_periods, color = number_periods), size = .4, linewidth = .70, position = position_dodge(width = .2)) +
  ylim(-.1, .25) +
  scale_color_manual(values = c("black", "darkgray")) +
  theme_minimal() +
  theme(legend.position = "bottom",
        axis.text.x = element_text(color = "black", size = 10),
        axis.title.y = element_text(size = 10),
        legend.title = element_text(size = 10)) +
  labs(y = "Effect of (Placebo) Democratic Transition",
       x = "",
       color = "Years before Actual Transition (Pooled)",
       shape = "Years before Actual Transition (Pooled)",
       title = "Panel B: Placebo Treatment Tests")


#### save ggplot object to combine into one figure ####
write_rds(id_panel_b, "data/identification-assumptions-panel-b.rds")
